Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
'Imports DotNetNuke.Installer
Imports ICSharpCode.SharpZipLib.Zip
Imports System.IO
Imports DotNetNuke
Imports System.Net

Namespace NVPortal.Modules.NVNews

    Public MustInherit Class ViewProfile
        Inherits Entities.Modules.PortalModuleBase
        Dim itemid As Integer = -1

#Region "Event Handlers"
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            binddata()
            'Response.Write(PortalSettings.HomeDirectory)
            Try
                If Request.Item("userid") <> "" Then
                    itemid = CInt(Request.Item("userid"))
                End If
                If Not IsPostBack Then
                    '<Edit news>
                    If itemid <> -1 Then
                        ' Hien thi tin nay
                        Dim objUser As UserInfo
                        objUser = UserController.GetUserById(0, itemid)
                        Me.UserName.Text = objUser.Username
                        Me.DisplayName.Text = objUser.DisplayName
                        Me.firstname.Text = objUser.FirstName
                        Me.lastname.Text = objUser.LastName
                        Me.email.Text = objUser.Email
                        Me.mobile.Text = objUser.Profile.Cell
                        Me.website.Text = objUser.Profile.Website
                        Me.ym.Text = objUser.Profile.IM
                        Me.hplEditUser.NavigateUrl = "/suathongtincanhan/" & UserId & "/" & ReplaceChuoi(Me.UserName.Text) & ".htm"
                        If UserId = objUser.UserID Then
                            Me.CheckUserId.Visible = True
                        Else
                            Me.CheckUserId.Visible = False
                        End If
                        Dim ctlNews As New NV_NewsController
                        Dim strStartDate As String = "1/1/1900"
                        Dim strEndDate As String = "1/1/2100"
                        If Me.txtStartdate.Text <> "" Then
                            strStartDate = Me.txtStartdate.Text
                        End If
                        If Me.txtEndDate.Text <> "" Then
                            strEndDate = Me.txtEndDate.Text
                        End If

                        Dim ds As ArrayList
                        ds = ctlNews.FindByStatus(strStartDate, strEndDate, Trim(Me.txtTitle.Text), Me.ddlCategory.SelectedValue, PortalId, 2, objUser.UserID)
                        Me.drgDataViewer.DataSource = ds
                        Me.drgDataViewer.PageSize = Me.ddlPageSize.SelectedValue
                        Me.drgDataViewer.DataBind()
                        Me.lbTotalNewsCount.Text = ds.Count
                    End If
                End If
                '</Edit news>
            Catch ex As Exception
                ProcessModuleLoadException(Me, ex)
            End Try

        End Sub
        Public Function GetUser(ByVal id As Integer) As UserInfo
            Dim objuser As UserInfo
            objuser = UserController.GetUserById(PortalId, UserId)
            Return objuser
        End Function
        Public Function ReplaceChuoi(ByVal chuoi As String) As String
            Dim convert As String = Global.ReplaceChuoi.bodau(chuoi).Replace(" ", "-")
            Return convert
        End Function
        Private Sub binddata()
            BindddlCategories()
            'BinddrgDataViewer()
        End Sub
        Private Sub BinddrgDataViewer()
            Try
            Catch ex As Exception
                Response.Write(ex.ToString)
            End Try
        End Sub

        Private Sub BindddlCategories()
            Dim ctlNewsCategories As New NV_NewsCategoriesController
            Dim arrNewsCategories As New ArrayList
            arrNewsCategories = ctlNewsCategories.GetAll(PortalId)
            Dim arrTemp As New ArrayList
            Dim objNewsCategories As NV_NewsCategoriesInfo
            Dim objNewsCategoriesTemp As NV_NewsCategoriesInfo

            If arrNewsCategories.Count > 0 Then
                For Each objNewsCategories In arrNewsCategories
                    If objNewsCategories.ParentId = 0 Then
                        arrTemp.Add(objNewsCategories)
                        For Each objNewsCategoriesTemp In arrNewsCategories
                            If objNewsCategoriesTemp.ParentId = objNewsCategories.CategoryID Then
                                objNewsCategoriesTemp.CategoryName = "--" & objNewsCategoriesTemp.CategoryName
                                arrTemp.Add(objNewsCategoriesTemp)
                            End If
                        Next
                    End If
                Next
            End If

            Me.ddlCategory.DataSource = arrTemp
            Me.ddlCategory.DataTextField = "CategoryName"
            Me.ddlCategory.DataValueField = "CategoryId"
            Me.ddlCategory.DataBind()
            Me.ddlCategory.Items.Insert(0, New ListItem("--Tất cả thư mục--", 0))

        End Sub
        Private Sub lbtFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtFind.Click
            Try
                ' default find all new that need to approve
                Me.drgDataViewer.CurrentPageIndex = 0
                BinddrgDataViewer()
            Catch ex As Exception
                ProcessModuleLoadException(Me, ex)
            End Try
        End Sub
        Protected Sub drgDataViewer_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles drgDataViewer.PageIndexChanged
            Try

                Me.drgDataViewer.CurrentPageIndex = e.NewPageIndex

            Catch ex As Exception
                Me.drgDataViewer.CurrentPageIndex = 0

            End Try
            BinddrgDataViewer()
        End Sub
        Protected Sub ddlPageSize_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlPageSize.SelectedIndexChanged
            Me.drgDataViewer.CurrentPageIndex = 0
            BinddrgDataViewer()
        End Sub
        Public Function GetUserName(ByVal userid As Integer) As String
            If userid = -1 Then
                GetUserName = "Không có"
            Else
                Return UserController.GetUser(PortalId, userid, True).Username
            End If
        End Function
#End Region


    End Class

End Namespace
